package com.code.apollo.core;

import com.code.apollo.pojo.Configs;
import com.code.apollo.pojo.DB;
import com.code.apollo.pojo.Table;
import com.code.apollo.utils.DBUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.File;
import java.util.*;

/**
 * <Description>
 * 生成器接收类
 * @author Jun Lee
 * @version 1.0
 * @createDate 2020/03/16 12:51
 * @see com.code.apollo.core
 */
@Data
@Builder
@AllArgsConstructor
public class GeneratorBuilder {
    private String templatePath;
    private String outPath;
    private Configs configs;
    private DB db;

    public void generatorByTable() throws Exception {
        List<Table> dbInfo = DBUtil.getDbInfo(db);
        File file = new File(templatePath);
        Map map = new HashMap();
        map.put("date",DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        map.put("configs",configs);
        map.put("outPath",outPath);
        for (Table table : dbInfo) {
            map.put("table",table);
            FreeMarkerConfiguration.foreachGenerator(map,file);
        }
    }

}
